package com.pzhu.dormitory.controller;

import com.pzhu.dormitory.entity.CostEntity;
import com.pzhu.dormitory.entity.CostVo;
import com.pzhu.dormitory.service.CostService;
import com.pzhu.dormitory.utils.PageUtils;
import com.pzhu.dormitory.utils.R;
import com.pzhu.dormitory.utils.Rb;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * @author
 * @email
 * @date 2021-05-01 15:11:29
 */
@Slf4j
@RestController
@Api(value = "CostController", tags = "")
@RequestMapping("dormitory/cost")
public class CostController {
    @Autowired
    private CostService costService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取list")
    public R<CostEntity> list(@RequestParam Map<String, Object> params) {
        PageUtils<CostEntity> page = costService.queryPage(params);
        return Rb.a(CostEntity.class).success().page(page).build();
    }


    /**
     * 列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "page")
    public R<CostVo> page(@RequestParam Map<String, Object> params) {
        PageUtils<CostVo> page = costService.queryPageCostVo(params);
        return Rb.a(CostVo.class).success().page(page).build();
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取一个信息")
    public R<CostEntity> info(@Validated @PathVariable("id") Long id) {
        CostEntity cost = costService.getById(id);
        return Rb.a(CostEntity.class).success().withData(cost).build();
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存信息")
    @PostMapping("/save")
    public R<CostEntity> save(@Validated @RequestBody CostEntity cost) {
        costService.save(cost);
        return Rb.a(CostEntity.class).success().build();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新信息")
    @PutMapping("/update")
    public R<CostEntity> update(@RequestBody CostEntity cost) {
        CostEntity costEntity = costService.getById(cost.getId());
        cost.setCost(cost.getCost().add(costEntity.getCost()));
        costService.updateById(cost);
        return Rb.a(CostEntity.class).success().build();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除信息")
    @DeleteMapping("/delete")
    public R<CostEntity> delete(@RequestBody Long[] ids) {
        costService.removeByIds(Arrays.asList(ids));
        return Rb.a(CostEntity.class).success().build();
    }

}
